/*####################################################################################
  CLASS : qDojo.dijix.SimpleGrid
  ==================================
  - A simple grid widget
  
  - It is a SubView, can be inserted into ViewPanel directly
  
  - It is created when ViewPanel to which it belongs to is activated - 
  	by invokation of ViewPanel.init function
  	
  - Implements: subView.render() and subView.clean();
   
  By: P.M.Shrestha  
  ####################################################################################*/
dojo.provide("qDojo.dijix.SimpleGrid");

dojo.require("dojox.grid.DataGrid");
dojo.require("dijit.layout.ContentPane");
dojo.require("dojo.data.ItemFileWriteStore");

dojo.declare("qDojo.dijix.SimpleGrid", dijit.layout.ContentPane , {

		data 		: [ ],
		gridStruct 	: [	{name: 'Name' }, 
						{name: 'Last Name' }, 
						{name: 'Address' }, 
						{name: 'Phone'}, 
						{name: 'E-Mail'}
					  ],
		grid 		: null,
		// ======================================================================
		//	Post create widget - Create grid widget inside the CP
		// ----------------------------------------------------------------------
		postCreate: function() {		
			this.inherited(arguments);				
			// create grid
			//var model = new dojox.grid.DataGrid.Table(null, []);
			test_store = new dojo.data.ItemFileWriteStore({data: []});
			var layout = [ { cells: [ this.gridStruct ] } ];

			this.grid = new dojox.grid.DataGrid(
			{
				"id": this.id+"_grid",			
				"store": test_store,
				"structure": layout	,
				"style": "height:100%;width:100%" 
			});
			
			this.domNode.appendChild(this.grid.domNode);
			this.grid.render();
			this.startup();									
			var doo = function( _this , grid){ grid.onCellDblClick = function(e){ _this.onDoubleClick(e); }};				
			doo(this , this.grid);				
		},
		// ======================================================================
		//	Render response from server 
		// ----------------------------------------------------------------------	
		render: function(response,event){
			this.renderGrid(response);
		},
		// ======================================================================
		//	Clean up this widget
		// ----------------------------------------------------------------------	
		clean: function(){		
			console.log("Running SimpleGrid.clean() function.id:" + this.id);                      	        	        	          	
		//	this.grid.destroyDescendants();			
		//	this.grid.destroy();
		},
		// ======================================================================
		//	Re-Structure Grid Header
		// ----------------------------------------------------------------------
		reStruct: function(struct){			
			this.gridStruct = struct;
			this.grid.setStructure( [ { cells: [ this.gridStruct ] } ] );			
		},
		// ======================================================================
		//	Render grid with given data structure
		// ----------------------------------------------------------------------
		renderGrid: function(data){
			if(data)
				this.data = data;
			//var model = new dojox.grid.DataGrid.Table(null, this.data);
			//this.grid.setModel(model);
			this.grid.refresh();
		},
		// ======================================================================
		//	Add Row to Grid
		// ----------------------------------------------------------------------		
		addRow: function(rows){
			var g = this.grid;
			dojo.forEach(rows, function(row){ g.addRow(row); });	
		},
		// ======================================================================
		//	Clear data in grid
		// ----------------------------------------------------------------------	
		clearGrid: function(){			
			while(this.grid.model.count > 0){
				this.grid.selection.select(0);
				this.grid.removeSelectedRows();	
			}				
		},
		// ======================================================================
		//	Double Click Callback
		// ----------------------------------------------------------------------	
		onDoubleClick: function(e){
			//console.log("Item " + e.rowIndex + " double clicked");	
		},
		// ======================================================================
		//	Get Value at Cell
		// ----------------------------------------------------------------------	
		getCellValue: function(row,col){
			return this.grid.model.getDatum(row,col)
		}		
		
});


